package com.paomi.client.activity.person;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.activity.detail.model.Comment;
import com.paomi.client.activity.person.Adapter.MyCommentListAdapter;
import com.paomi.client.activity.person.View.MyCommentsView;
import com.paomi.client.activity.person.model.MyCommentBean;
import com.paomi.client.activity.person.presenter.MyCommentPre;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Constants;
import com.paomi.client.refresh.AdapterWrapper;
import com.paomi.client.refresh.SwipeToLoadHelper;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.UIUtils;
import com.paomi.client.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyCommentsActivity extends BaseMvpActivity<MyCommentsView, MyCommentPre> implements MyCommentsView, SwipeToLoadHelper.LoadMoreListener {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    MyCommentListAdapter adapter;

    List<MyCommentBean.Data> list = new ArrayList<>();
    Dialog dialog;
    @BindView(R.id.ll_none)
    LinearLayout llNone;
    //添加上拉加载
    private AdapterWrapper mAdapterWrapper;
    private SwipeToLoadHelper mLoadMoreHelper;
    private int pageNum = 1;

    @NonNull
    @Override
    public MyCommentPre createPresenter() {
        return new MyCommentPre();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.string_my_comment);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));

    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_comments;
    }

    @Override
    protected void initEvent() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyCommentListAdapter(this);
        mAdapterWrapper = new AdapterWrapper(adapter);
        mLoadMoreHelper = new SwipeToLoadHelper(recyclerView, mAdapterWrapper);
        mLoadMoreHelper.setLoadMoreListener(this);
        recyclerView.setAdapter(mAdapterWrapper);

        getPresenter().getComments(pageNum);

        adapter.ClickView(new MyCommentListAdapter.ClickView() {
            @Override
            public void delete(String id, int pos) {

                LayoutInflater inflater = LayoutInflater.from(MyCommentsActivity.this);
                View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
                dialog = new Dialog(MyCommentsActivity.this, R.style.theme_dialog);
                dialog.setContentView(view);
                dialog.show();
                TextView textCancel = view.findViewById(R.id.btn_qd);
                textCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                TextView textSure = view.findViewById(R.id.btn_success);
                textSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        getPresenter().deleteCommente(id, pos);

                    }
                });

            }

            @Override
            public void toShow(String goodId) {
                Intent i = new Intent(MyCommentsActivity.this, DetailTicketActivity.class);
                i.putExtra("getId", goodId);
                startActivity(i);
            }
        });
    }


    @Override
    public void showSuccessList(MyCommentBean bean) {
        if (bean.getReturnData() != null) {
            if (bean.returnCode.equals("0000")) {
                if (pageNum == 1) {
                    list.clear();
                }
                if(list.size()>0){
                    //判断是否有重复数据
                    boolean isData=false;
                    List<MyCommentBean.Data>  comments1=bean.getReturnData().getData();
                    for(MyCommentBean.Data comment:comments1){
                        for(int i=0;i<list.size();i++){
                            if(comment.getId() == list.get(i).getId()){
                                isData=false;
                                break;
                            }else{
                                isData=true;
                            }
                        }
                    }
                    if(isData){
                        list.addAll(bean.getReturnData().getData());
                    }else{
                        ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.data_null));
                    }
                }else{
                    list.addAll(bean.getReturnData().getData());
                }
//                list.addAll(bean.getReturnData().getData());
                adapter.setData(list);
            } else {
                Util.toast(this, bean.returnMessage);
            }
            if (bean.getReturnData().getData().size() == 0) {
                llNone.setVisibility(View.VISIBLE);
            } else {
                llNone.setVisibility(View.GONE);
            }
        } else {
            llNone.setVisibility(View.VISIBLE);
        }
        if (list.size() % Constants.PAGE_SIZE != 0) {//移除FooterView
            mAdapterWrapper.setLoadItemVisibility(false);
        } else {
            mAdapterWrapper.setLoadItemVisibility(true);
        }
        mLoadMoreHelper.setLoadMoreFinish();
        mAdapterWrapper.notifyDataSetChanged();

    }

    @Override
    public void deleteSuccess(BaseJSON result, int pos) {
        if (result.returnCode.equals("0000")) {
            list.remove(pos);
            adapter.notifyDataSetChanged();
            mAdapterWrapper.notifyDataSetChanged();
            Util.toast(this, "删除成功");
        } else {
            Util.toast(this, result.returnMessage);
        }
    }

    @Override
    public void onLoad() {
        if (list.size() % Constants.PAGE_SIZE == 0) {//是10的倍数
            pageNum++;
            getPresenter().getComments(pageNum);
        } else {
            mAdapterWrapper.setLoadItemVisibility(false);
            mLoadMoreHelper.setLoadMoreFinish();
        }
    }
}
